package com.joe.gulishop.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.joe.gulishop.product.vo.AttrResponseVO;
import com.joe.gulishop.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.joe.gulishop.product.entity.AttrEntity;
import com.joe.gulishop.product.service.AttrService;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.R;



/**
 * 商品属性
 *
 * @author joe
 * @email joe@gmail.com
 * @date 2021-12-03 10:45:56
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据ID查询信息，并且查询出改Id所在的分组
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
       AttrResponseVO attrVo =attrService.getAttrInfo(attrId);


        return R.ok().put("attr", attrVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttrById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

// http://localhost:88/api/product/attr/sale/list/0?t=1642821250466&page=1&limit=10&key=
   // http://localhost:88/api/product/attr/base/list/0?t=1642821445137&page=1&limit=10&key=
    //他们的区别就是类型的不一致
    @GetMapping("/{attrType}/list/{catelogId}")
    public R attrBaseList(@PathVariable("catelogId")Long catelogId,
                          @PathVariable("attrType")String attrType,
                          @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page",pageUtils);
    }


}
